package com.genius.reservation.tools;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.tools.annotation.webLog;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

public class WxUtil {

    public static ResponseRule getOpenId(String code){
        HashMap<String, Object> parm = new HashMap<>();
        parm.put("appid","wxc56c0a8db5f95e1e");
        parm.put("secret","7a398709277f37e06789e3a552980b72");
        parm.put("js_code",code);
        parm.put("grant_type","authorization_code");
        try {
            String s = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", parm);
            return ResponseRule.ok(JSONObject.parseObject(s));
        }catch (Exception e){
            e.getStackTrace();
            throw  new RuntimeException(e.getMessage());
        }
        /*
        *
        * wx.cloud.callContainer({
  "config": {
    "env": "prod-8gfehdscb8465c6f"
  },
  "path": "/api/count",
  "header": {
    "X-WX-SERVICE": "express-2lxb"
  },
  "method": "POST",
  "data": {
    "action": "inc"
  }
})*/
    }

    public static JSONObject getAccessToken(){
        try {
            String s = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxef30e257d686e58f&secret=f926fc51967c95e8d02cbdfab067a90b");
            return JSONObject.parseObject(s);
        }catch (Exception e){
            e.getStackTrace();
            throw  new RuntimeException(e.getMessage());
        }
    }
    public static JSONObject GetUnlimited(String scene, String page){
        HashMap<String, Object> parm = new HashMap<>();
        parm.put("scene",scene);
        parm.put("page",page);
        try {
            String s = HttpUtil.get("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+getAccessToken().getString("access_token"), parm);
            return JSONObject.parseObject(s);
        }catch (Exception e){
            e.getStackTrace();
            throw  new RuntimeException(e.getMessage());
        }
    }
}
